package com.headwind.logistics.waybill.service.impl;

import com.headwind.logistics.common.pojo.BaseNetSite;
import com.headwind.logistics.common.pojo.ExpressGrant;
import com.headwind.logistics.waybill.dto.ExpressGrantSiteEmpDTO;
import com.headwind.logistics.waybill.dto.ExpressRecordEmpDTO;
import com.headwind.logistics.waybill.mapper.ExpressGrantMapper;
import com.headwind.logistics.waybill.mapper.ExpressSheetMapper;
import com.headwind.logistics.waybill.service.intl.ExpressServiceIntl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 小恶魔
 * @Date 2020-07-21 20:24
 * @Version 1.0
 * @DESCRIPTION 快递单业务逻辑层实现类
 */
@Service
public class ExpressServiceImpl implements ExpressServiceIntl {

    //快递单号数据访问层
    @Resource
    private ExpressSheetMapper expressSheetMapper;

    //面单发放记录数据访问层
    @Resource
    private ExpressGrantMapper expressGrantMapper;
    /**
     * 获取快递单取号信息
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<ExpressRecordEmpDTO> queryExpressRecord(Integer page, Integer rows) {

        if (page!=null){
            page = (page-1)*rows;
        }

        return expressSheetMapper.queryExpressRecord(page, rows);
    }

    /**
     * 获取最新的快递单号
     * @return
     */
    @Override
    public Long queryNewExpressEndNum() {
        return expressSheetMapper.queryNewExpressEndNum();
    }

    /**
     * 添加快递单号
     * @param expressRecordEmpDTO
     * @return
     */
    @Override
    public Integer addExpressNum(ExpressRecordEmpDTO expressRecordEmpDTO) {
        return expressSheetMapper.addExpressNum(expressRecordEmpDTO);
    }

    /**
     * 批量删除快递取号表
     * @param expressIds
     * @return
     */
    @Override
    @Transactional
    public Integer batchDeleteExpressFetch(String expressIds) {

        String[] idList = expressIds.split("-");
        List<String> ids = new ArrayList<>();
        for (String id : idList) {
            //查询此面单取号 号端是否已经分发给网点
            Integer cou = expressSheetMapper.queryExpressFetchAndGrant(Long.parseLong(id));

            //小于1说明还未使用 既执行删除
            if (cou < 1) {
                ids.add(id);
            }

        }
        Integer count = 0;
        if (ids.size() != 0){
            //批量删除
            count = expressSheetMapper.batchDeleteExpressFetchRecordById(ids);
        }


        return count;
    }

    /**
     * 根据行政区域id 获取 具体网点营业信息
     * @param areaId
     * @return
     */
    @Override
    public List<BaseNetSite> queryNetAreaSiteByAreaId(Integer areaId) {
        return expressSheetMapper.queryNetAreaSiteByAreaId(areaId);
    }

    /**
     * 添加面单记录表
     * @param expressGrant
     * @return
     */
    @Override
    public Integer addExpressGrant(ExpressGrant expressGrant) {

        //赋值已使用单号数量
        expressGrant.setYUse(0);
        //赋值未使用单号数量
        expressGrant.setNoUse(expressGrant.getGrantCount());
        expressGrant.setGrantType(0);
        Integer count = expressSheetMapper.addExpressGrant(expressGrant);
        expressSheetMapper.updateExpressGrantStartById(2, expressGrant.getExpressRecordId());

        return count;

    }

    /**
     * 修改快递单号 取号表状态
     * @param start
     * @param expressRecordId
     * @return
     */
    @Override
    public Integer updateExpressGrantStartById(Integer start, Long expressRecordId) {
        return expressSheetMapper.updateExpressGrantStartById(start, expressRecordId);
    }

    /**
     * 获取面单发放记录
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<ExpressGrantSiteEmpDTO> queryAllExpressGrantOrPage(Integer page, Integer rows) {

        if (page != null){
            page = (page-1)*rows;
        }

        List<ExpressGrantSiteEmpDTO> expressGrantSiteEmpDTOS = expressGrantMapper.queryAllExpressGrantOrPage(page, rows);
        return expressGrantSiteEmpDTOS;
    }

    /**
     * 根据发放日期和网点名称查询面单发放记录
     * @param page
     * @param rows
     * @param startDate
     * @param endDate
     * @param siteName
     * @return
     */
    @Override
    public List<ExpressGrantSiteEmpDTO> queryExpressGrantByGrantDateOrSiteName(Integer page, Integer rows, String startDate, String endDate, String siteName) {
        if (page !=null){
            page = (page-1)*rows;
        }
        return expressGrantMapper.queryExpressGrantByGrantDateOrSiteName(page, rows, startDate, endDate, siteName);
    }
}
